** This Do-File replicates Tables A3 and A4 from P. Dutronc, A. Tondini "Large Means-Tested Pensions with Informal Labor Markets" ** 

set more off

cd "" /*Input here directory where the PALMS v3.2 (only years from 2002--2015) is stored */ 

use "south_africa_palms.dta", clear

**************************************
** Select Population for Estimation ** 
**************************************

drop if year==2008 | year==2009 /* Exclude years in which the threshold is lowered from 65 to 60 */ 

keep if pop_group==1 | pop_group==2  /* Keep Black and Coloured individuals only, see text for explanation */ 

drop if sex==. | sex==9 /* Exclude observations with missing value for gender */ 

gen married=(marital_status==1) /* 1/0 variable for marital status */ 

keep if age>=50 & age<70 /*Window +/- 5 */ 


** Adjust weights for multiple year estimation ** 

bys year: egen mean_weight=mean(weight) 
replace weight=weight/mean_weight /* Adjust weights to have the same mean across waves */

** Definition of Informal Employment **

gen tot_inf=1 if status==1 & no_written_contract==1 /*Employees w/o written contract */
replace tot_inf=0 if status==1 & no_written_contract==0  /*Employees w. written contract */
replace tot_inf=1 if status==2 & not_registered==1 /*Self-employed w/o registered business*/
replace tot_inf=0 if status==2 & not_registered==0 /*Self-employed w. registered business*/
replace tot_inf=0 if status==3 | status==4 /*Not Employed*/ 

keep if tot_inf!=. /* Exclude from the estimation those with missing informality status.*/

gen f_emp=(tot_inf==0 & emp==1)


**  Wages and Hours ** 

replace hrs_wrk=0 if status==3 | status==4 /* Set hours equal to zero if not employed */
replace monthly_sal=0 if status==3 | status==4 /* Set monthly salary equal to zero if not employed */

gen hrs_wrk_f=hrs_wrk if f_emp==1 /* hours of work in formal employment */
replace hrs_wrk_f=0 if f_emp==0 /* hours of work in formal employment */

gen hourly_wage=monthly_sal/(hrs_wrk*4.5)

replace monthly_sal=ln(monthly_sal) 
replace hourly_wage=ln(hourly_wage)


drop if hourly_wage==. & emp==1 /* Exclude if salary or hours information is missing */ 


**************************************
************ Regressions ** **********
**************************************
gen OAP_1=(age>=60) /* Indicator variable for being at or above the threshold */ 


gen x=age-60 /* center discontinuity at 0 */ 
gen x2=x^2 

gen post=(year>2008) 

gen yob=year-age  

egen cluster=group(yob pop_group) /* Cluster for standard errors by race*cohort group to account for repeated observations over time in DiDisc estimation, see text for explanation */ 




*************
**Table A3 **
*************

************************************************************************************

local f_form x x2 /*Linear Functional Form */ 
*local controls i.Province i.married i.education /* set of control variables */ 

** Males ** 

reg emp  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1 `controls'  [w=weight] if sex==1 & post==0, robust
outreg2 using tableA3_male.tex, tex  replace label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)
reg tot_inf  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1 `controls'  [w=weight] if sex==1 & post==0, robust
outreg2 using tableA3_male.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)
reg f_emp  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1 `controls'  [w=weight] if sex==1 & post==0, robust
outreg2 using tableA3_male.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)
reg emp  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1 `controls'  [w=weight] if sex==1 & post==1, robust
outreg2 using tableA3_male.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)
reg tot_inf  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1 `controls'  [w=weight] if sex==1 & post==1, robust
outreg2 using tableA3_male.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)
reg f_emp  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1 `controls'  [w=weight] if sex==1 & post==1, robust
outreg2 using tableA3_male.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)

table post [aw=weight] if age==59 & sex==1, c(mean emp mean tot_inf mean f_emp)

** Females ** 

reg emp  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1 `controls'  [w=weight] if sex==2 & post==0, robust
outreg2 using tableA3_female.tex, tex  replace label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)
reg tot_inf  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1 `controls'  [w=weight] if sex==2 & post==0, robust
outreg2 using tableA3_female.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)
reg f_emp  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1  `controls' [w=weight] if sex==2 & post==0, robust
outreg2 using tableA3_female.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)
reg emp  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1 `controls'  [w=weight] if sex==2 & post==1, robust
outreg2 using tableA3_female.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)
reg tot_inf  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1 `controls'  [w=weight] if sex==2 & post==1, robust
outreg2 using tableA3_female.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)
reg f_emp  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1 `controls'  [w=weight] if sex==2 & post==1, robust
outreg2 using tableA3_female.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)

table post [aw=weight] if age==59 & sex==2, c(mean emp mean tot_inf mean f_emp)

************************************************************************************

erase tableA3_male.tex 
erase tableA3_female.tex
erase tableA3_male.txt 
erase tableA3_female.txt



*************
**Table A4 **
*************

************************************************************************************

local f_form x x2 /*Linear Functional Form */ 
*local controls i.Province i.married i.education /* set of control variables */ 

** Males ** 

reg emp  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==1, cluster(cluster)
outreg2 using tableA4_male.tex, tex  replace label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post) 
reg tot_inf  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==1, cluster(cluster)
outreg2 using tableA4_male.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post) 
reg f_emp  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==1, cluster(cluster)
outreg2 using tableA4_male.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post)
reg hrs_wrk_f  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==1, cluster(cluster)
outreg2 using tableA4_male.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post)

table post [aw=weight] if age==59 & sex==1, c(mean emp mean tot_inf mean f_emp mean hrs_wrk_f)


** Females ** 

reg emp i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==2, cluster(cluster)
outreg2 using tableA4_female.tex, tex  replace label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post) 
reg tot_inf i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==2, cluster(cluster)
outreg2 using tableA4_female.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post) 
reg f_emp i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==2, cluster(cluster)
outreg2 using tableA4_female.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post)
reg hrs_wrk_f i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==2, cluster(cluster)
outreg2 using tableA4_female.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post)

table post [aw=weight] if age==59 & sex==2, c(mean emp mean tot_inf mean f_emp mean hrs_wrk_f)

************************************************************************************


erase tableA4_male.tex 
erase tableA4_female.tex
erase tableA4_male.txt 
erase tableA4_female.txt

